package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.Info.AllSeverityPageInfo;
import com.tracesystem.controller.Info.AllTypePageInfo;
import com.tracesystem.controller.form.*;
import com.tracesystem.model.IssueSeverity;
import com.tracesystem.dao.IssueSeverityDao;
import com.tracesystem.model.IssueType;
import com.tracesystem.service.IssueSeverityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.IssueSeverityException;
import com.tracesystem.util.exception.IssueTypeException;
import com.tracesystem.util.exception.code.IssueSeverityExceptionCode;
import com.tracesystem.util.exception.code.IssueTypeExceptionCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssueSeverityServiceImpl extends ServiceImpl<IssueSeverityDao, IssueSeverity> implements IssueSeverityService {
    //严重程度实现层对象
    @Resource
    private IssueSeverityService issueSeverityService;
    //严重程度分类业务层对象
    @Resource
    private IssueSeverityDao issueSeverityDao;
    /**
     * 修改严重程度分类
     * @param renewalSeverityDescForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> updateSeverityDesc(RenewalSeverityDescForm renewalSeverityDescForm) {
        IssueSeverity issueSeverity = issueSeverityService.getOne(//查询类型是否存在
                new QueryWrapper<IssueSeverity>().eq("SEVERITY_CODE", renewalSeverityDescForm.getSeverityCode()));
        if(null==issueSeverity) {
            throw new IssueSeverityException(IssueSeverityExceptionCode.IssueSeverity_NOTEXIST_ERROR);
        }else {
            if (issueSeverity.getSeverityDesc().equals(renewalSeverityDescForm.getSeverityDesc())){
                throw new IssueSeverityException(IssueSeverityExceptionCode.IssueSeverity_EXIST_ERROR);
            }else {
                boolean b = issueSeverityService.saveOrUpdate(WoniuCopyUtil.copyOne(renewalSeverityDescForm, IssueSeverity.class));
                if(!b) return Result.getInstance().setMsg("严重程度编号不存在,已经为您新增该严重程度分类");
                return Result.getInstance().setMsg("修改严重程度分类成功");
            }
        }
    }

    /**
     * 修改严重程度状态
     * @param renewalSeverityModuleStateForm
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> updateSeverityModuleState(RenewalSeverityModuleStateForm renewalSeverityModuleStateForm) {
        IssueSeverity issueSeverity = issueSeverityService.getOne(//查询类型是否存在
                new QueryWrapper<IssueSeverity>().eq("SEVERITY_CODE", renewalSeverityModuleStateForm.getSeverityCode()));
        if (issueSeverity.getModuleState().equals("有效")){
            issueSeverity.setModuleState("禁用");
        }else {
            issueSeverity.setModuleState("有效");
        }
        int i = issueSeverityDao.updateById(WoniuCopyUtil.copyOne(issueSeverity, IssueSeverity.class));
        if(i>0) return Result.getInstance().setMsg("修改严重程度状态成功");
        return Result.getInstance().setCode(50011).setMsg("修改严重程度状态失败");
    }

    /**
     * 根据条件模糊查询严重程度分类
     * @param likeSeverityListForm
     * @return
     */
    @Override
    public Result<AllSeverityPageInfo> findPageSeverityListBySeverityDesc(LikeSeverityListForm likeSeverityListForm) {
        QueryWrapper<IssueSeverity> wrapper = new QueryWrapper<IssueSeverity>().orderByDesc("SEVERITY_CODE");
        if(likeSeverityListForm.getSeverityDesc() != null && !likeSeverityListForm.getSeverityDesc().trim().equals(""))
            wrapper.like("SEVERITY_DESC",likeSeverityListForm.getSeverityDesc());

        Page<IssueSeverity> wa = page(new Page<>(likeSeverityListForm.getPageIndex(),likeSeverityListForm.getPageSize()),wrapper);
        List<AllSeverityPageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllSeverityPageInfo.class);

        Page<AllSeverityPageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");

    }

    /**
     * 查询全部严重程度分类
     * @param findAllSeverityListForm
     * @return
     */
    @Override
    public Result<AllSeverityPageInfo> getAllTypeList(FindAllSeverityListForm findAllSeverityListForm) {
        QueryWrapper<IssueSeverity> wrapper = new QueryWrapper<IssueSeverity>().orderByDesc("SEVERITY_CODE");

        Page<IssueSeverity> wa = page(new Page<>(findAllSeverityListForm.getPageIndex(),findAllSeverityListForm.getPageSize()),wrapper);
        List<AllSeverityPageInfo> pageInfoList = WoniuCopyUtil.copyMany(wa.getRecords(), AllSeverityPageInfo.class);

        Page<AllSeverityPageInfo> page = new Page<>();
        page.setRecords(pageInfoList)
                .setTotal(wa.getTotal())
                .setCurrent(wa.getCurrent())
                .setSize(wa.getSize())
                .setPages(wa.getPages());
        return Result.getInstance(Page.class).setData(page).setMsg("查询成功");
    }

    //添加
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> insertSeverity(AddSeverityForm addSeverityForm) {
        IssueSeverity issueSeverity = issueSeverityService.getOne(//查询严重程度是否存在
                new QueryWrapper<IssueSeverity>().eq("SEVERITY_DESC", addSeverityForm.getSeverityDesc()));
        //存在则抛出已存在异常
        if(null!=issueSeverity) throw new IssueSeverityException(IssueSeverityExceptionCode.IssueSeverity_EXIST_ERROR);
//        addSeverityForm.setModuleState("0");//默认值为0
        issueSeverityService.save(WoniuCopyUtil.copyOne(addSeverityForm,IssueSeverity.class));
        return Result.getInstance().setMsg("新增严重程度分类成功");
    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectSeverity(RemoveSeverityForm removeSeverityForm) {
        boolean b = issueSeverityService.removeById(removeSeverityForm.getSeverityCode());
        if(!b) throw new IssueSeverityException(IssueSeverityExceptionCode.IssueSeverity_NOTEXIST_ERROR);
        return Result.getInstance().setMsg("删除严重程度分类成功");
    }

    //批量删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> delectSeverityList(RemoveSeverityListForm removeSeverityListForm) {
        List<Integer> severityCodes = removeSeverityListForm.getSeverityCodes();
        for (Integer severity : severityCodes) {
            boolean b = issueSeverityService.removeById(severity);
            if(!b) throw new IssueSeverityException(IssueSeverityExceptionCode.IssueSeverity_NOTEXIST_ERROR);
            continue;
        }
        return Result.getInstance().setMsg("删除严重程度分类成功");
    }

}
